import { createI18n } from 'vue-i18n'
import { Locale } from 'vant'
import enUS from 'vant/es/locale/lang/en-US'
import zhCN from 'vant/es/locale/lang/zh-CN'

import enLocale from './lang/en_US'//英语
import zhLocale from './lang/zh_CN'//简体
const messages = {
    en: {
        ...enUS,
        ...enLocale
    },
    zh: {
        ...zhCN,
        ...zhLocale
    },
}

export function getLanguage() {
    const chooseLanguage = localStorage.getItem('languageStatus')
    if (chooseLanguage) return chooseLanguage
    // if has not choose language 
    const language = (navigator.language || navigator.browserLanguage).toLowerCase()
    const locales = Object.keys(messages)
    //跟随浏览器语言
    for (const locale of locales) {
        if (language.indexOf(locale) > -1) {
            return locale
        }
    }
    return 'zh'
}
const i18n = new createI18n({
    locale: getLanguage(),  // 设置默认语言
    messages: messages // 设置资源文件对象
})

// 更新vant组件库本身的语言变化，支持国际化
function vantLocales(lang) {
    switch (lang) {
        case 'en':
            Locale.use(lang, enUS)
            break;
        default:
            Locale.use(lang, zhCN)
            break;
    }
}

export { i18n, vantLocales }